import { defineStore } from 'pinia';
import { ref } from 'vue';
import { WS_BASE_URL } from '@/config';

export const useWebSocketStore = defineStore('websocket', () => {
  const ws = ref<WebSocket | null>(null);

  const connect = (userId: string) => {
    const wsUrl = `${WS_BASE_URL}?userId=${userId}`;
    ws.value = new WebSocket(wsUrl);

    ws.value.onopen = () => {
      console.log('WebSocket 连接成功');
    };

    ws.value.onerror = (error) => {
      console.error('WebSocket 错误:', error);
    };

    ws.value.onclose = () => {
      console.log('WebSocket 连接关闭');
    };
  };

  const disconnect = () => {
    if (ws.value) {
      ws.value.close();
      ws.value = null;
    }
  };

  return {
    ws,
    connect,
    disconnect
  };
}); 